home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 566 < prev    next >
Encoding:
Text File  |  1996-08-06  |  6.3 KB  |  137 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de>
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Destruction of singletons
  5. Date: 27 Feb 1996 10:45:03 PST
  6. Organization: -
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <9602271124.AA18148@lts.sel.alcatel.de>
  9. References: <4fvng1$jit@netlab.cs.rpi.edu> <4go9au$l8f@engnews1.Eng.Sun.COM> <9602261828.AA28873@lts.sel.alcatel.de> <4gtmg0$t59@pentagon.io.com>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Tue, 27 Feb 96 12:24:42 +0100
  12. In-Reply-To: jamshid@io.com's message of 26 Feb 1996 18:08:20 PST
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMTNRR0y4NqrwXLNJAQGRCgH+Kxq9IZ+ecNCH2hQLvfk0SDGv9UAV9NxT
  15.     UvncC1mzxZERHQojhZsPQukpC5FgI5oR39Lms8ptN1c1hT1NIorkig==
  16.     =A/LF
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. In article <4gtmg0$t59@pentagon.io.com> jamshid@io.com (Jamshid
  20. Afshar) writes:
  21.  
  22. |> In article <9602261828.AA28873@lts.sel.alcatel.de>,
  23. |> James Kanze US/ESC 60/3/141 #40763  <kanze@lts.sel.alcatel.de> wrote:
  24. |> >In article <4go9au$l8f@engnews1.Eng.Sun.COM> Jamshid Afshar
  25. |> ><jamshid@IO.COM> writes:
  26. |> >
  27. |> >1. As long as there is an active pointer to the memory (a static
  28. |> >pointer), Purify will not classify it as a leak.
  29.  
  30. |> You're right.  I thought there was some kind of report of all unfreed
  31. |> memory at program exit.
  32.  
  33. I think you can get a report of all unfreed memory, too, although it
  34. isn't classified as a leak or a potential leak.
  35.  
  36. |> >In general, I would stress that the prefered solution is to not use
  37. |> >the singleton object in any destructors, and use a local static to
  38. |> >initialize the pointer.  If you do have to use the object in
  39. |> >destructors, however, I would still prefer not destructing it to
  40. |> >trying to manage the `order of destruction' problem (given that most
  41. |> >compiler implementors still consider the order of destruction of
  42. |> >static locals undefined).
  43.  
  44. |> I agree I would rather have the compiler manage the destruction,
  45. |> especially since it's so easy for them to do it (just generate a
  46. |> "destroy" function and register it with atexit() when the constructor
  47. |> returns).
  48.  
  49. This would be one solution.
  50.  
  51. All of the solutions involve dynamic ordering; i.e.: some sort of
  52. registration when the object is constructed.  At present, all of the
  53. compilers I know of use static ordering for the destructors, with just
  54. a simple flag to indicate whether the local static was constructed or
  55. not.
  56.  
  57. |> >|> Actually, the construction and destruction of "local objects of static
  58. |> >|> storage duration" is not the same as objects outside of functions.  In
  59. |> >|> fact, it seems local statics are more poorly specified.  According to
  60. |> >|> the April '95 Draft, while they are guaranteed to not be constructed
  61. |> >|> until (and if) the function is called, exactly when the destructor is
  62. |> >|> called is unspecified.
  63. |> >
  64. |> >I actually believe that it is, and in fact, has been well defined from
  65. |> >the ARM.
  66.  
  67. |> Okay, I see now that I was misreading section 6.7 Declaration
  68. |> Statement in the ARM and April 95 Draft.  I took the sentence "Exactly
  69. |> when is unspecified" to mean the destructor order is *completely*
  70. |> unspecified.  I guess the sentence is only referring to the previous
  71. |> sentence (ie, "Whether the destructor is called before or as part of
  72. |> the atexit() functions is unspecified.").
  73.  
  74. |>    5 The destructor for a local object with static storage duration will be
  75. |>      executed if and only if the variable was constructed.  The  destructor
  76. |>      is  called  either  immediately  before or as part of the calls of the
  77. |>      atexit()  functions  (_lib.support.start.term_).   Exactly   when   is
  78. |>      unspecified.
  79.  
  80. |> >In any case, the Jan. 96 draft makes the question moot, since it
  81. |> >explicitly adds the words in the destructor section clarifying that
  82. |> >static means both local and non-local.
  83.  
  84. |> I assume you're referring to 3.6.3 Termination (analogous to ARM 3.4
  85. |> Start and Termination).  Btw, has the wording in 6.7 been changed or
  86. |> improved?  There seems to be some conflict with it and 3.6.3 wrt the
  87. |> order of static destructors and atexit() calls.
  88.  
  89. Correct.  I've now got it on line, and in fact, it is better than I
  90. remembered, since it also definitly states that the order is
  91. determined by the completion of the constructors.  (For some reason, I
  92. remembered this as being vague.  I was wrong.)
  93.  
  94.     Destructors for initialized objects of static storage duration
  95.     (declared at block scope or at namespace scope) are called when
  96.     returning from <B>main</B> and when calling <B>exit</B>.  These
  97.     objects are destroyed in the reverse order of the completion of
  98.     their constructors.  For an object of array or class type, all
  99.     subobjects of that object are destroyed before any local object
  100.     with static storage duration initialized during the construction
  101.     the subobjects is destroyed.
  102.  
  103. Note the words in the first parentheses; this makes it quite clear
  104. that the ordering holds over both local (block scope) and non-local
  105. (namespace scope) variables.
  106.  
  107. |> >|> [...] What should
  108. |> >|> have been required IMO is that local statics be destroyed in the
  109. |> >|> reverse order they were (fully) constructed. 
  110. |> >
  111. |> >I agree.  The remaining weak point in the standard is that it doesn't
  112. |> >really specify what is meant by the ordering, entering the
  113. |> >constructor, or leaving it.
  114.  
  115. |> If the Standard does not require local statics to be destroyed in the
  116. |> reverse order they were *fully* constructed, local statics are useless
  117.     [...]
  118.  
  119. Agreed.  This was my mistake: the current (Jan. 96) draft does
  120. guarantee exactly what you (and I) want.
  121.  
  122. Now if I could only convince them to require static initialization (of
  123. non-local statics) to take place before main:-).
  124.  
  125. --
  126. James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
  127. GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
  128. Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  129.                 -- A la recherche d'une activitΘ dans une region francophone
  130. ---
  131. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  132.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  133.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  134.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  135.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  136. ]
  137.